$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/tcmeetingroom/list',
        datatype: "json",
        colModel: [
			{ label: '会议中心名称', name: 'conventionCenterName', index: 'CONVENTION_CENTER_NAME', width: 80 }, 			
			{ label: '会议室名称', name: 'name', index: 'NAME', width: 80 }, 			
			{ label: '会议中心位置', name: 'position', index: 'POSITION', width: 80 },
			{ label: '会议室容量（人数）', name: 'capacity', index: 'CAPACITY', width: 80 },
			{ label: '会议室描述', name: 'meetingRoomMemo', index: 'MEETING_ROOM_MEMO', width: 80 },
            { label: '会议室状态', name: 'status', index: 'STATUS', width: 80,formatter: function(value, options, row){
                    if(value == "NORMAL"){
                        return '<span class="label label-success">正常</span>'
                    }else if(value == "FORBIDDEN"){
                        return '<span class="label label-danger">禁用</span>'
                    }} },
			{ label: '创建时间', name: 'createTime', index: 'CREATE_TIME', width: 80 }, 			
			{ label: '修改时间', name: 'updateTime', index: 'UPDATE_TIME', width: 80 }, 			
			{ label: '备注', name: 'remark', index: 'REMARK', width: 80 }			
        ],
		viewrecords: true,
        height: 580,
        rowNum: 50,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		tcMeetingRoom: {conventionCenterName:null,conventionCenterId:null}
	},
	methods: {
        showMemberPage: function(event){

            layer.open({
                type: 2,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择会议中心",
                area: ['1100px', '500px'],
                shade: 0,
                shadeClose: true,
                content: 'http://localhost:8080/renren-admin/modules/sys/tcconventioncenter_load.html',
                btn: ['确定', '取消'],
                btn1: function (index,layero) {
                    var ibody = layer.getChildFrame ('body',index); //获取iframe页面body
                    var tr = ibody.find("#jqGrid tr[class*='success']");

                    var conventionCenterName = tr.find("td:eq(3)").text();
                    var position = tr.find("td:eq(4)").text();
                    var conventionCenterId = tr.find("td:eq(2)").text();
                    var status = tr.find("td:eq(5)").text();
                    if(tr.length>1){
                        alert("只能选择一个会议中心");
                        return;
                    } else if(tr.length==0){
                        alert("请选择一个会议中心");
                        return;
                    }
                    var sta = "禁用"
                    if (status==sta){
                        alert("请勿选择禁止的会议中心");
                        return;
                    }
                    /*alert(memberName);*/
                    vm.tcMeetingRoom.conventionCenterName=conventionCenterName;
                    vm.tcMeetingRoom.conventionCenterId=conventionCenterId;
                    vm.tcMeetingRoom.position=position;
                    $("#conventionCenterName").val(conventionCenterName);
                    $("#conventionCenterId").val(conventionCenterId);
                    $("#position").val(position);
                    layer.close(index);
                }
            });
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.tcMeetingRoom = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.tcMeetingRoom.id == null ? "sys/tcmeetingroom/save" : "sys/tcmeetingroom/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.tcMeetingRoom),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/tcmeetingroom/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/tcmeetingroom/info/"+id, function(r){
                vm.tcMeetingRoom = r.tcMeetingRoom;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});